<?php

class ajaxButton extends CButtonColumn {
    
    public $evaluateID = true;
    
    public function renderDataCellContent($row, $data)
    {
        $tr=array();
        ob_start();
        foreach($this->buttons as $id=>$button)
        {
            if($this->evaluateID and isset($button['options']['pl'])) 
            {
                $button['options']['pl'] = $this->evaluateExpression($button['options']['pl'], array('row'=>$row,'data'=>$data));
                $button['label'] = $button['options']['pl'] == 1 ? '<p class="btn btn-small"><i class=\'icon-ok\'></i></p>' : '<p class="btn btn-small"><i class=\'icon-lock\'></i></p>';
                $button['options']['data-original-title'] = $button['options']['pl'] == 1 ? 'Đã kích hoạt' : 'Đã khóa';
            }
 
            $this->renderButton($id,$button,$row,$data);
            $tr['{'.$id.'}']=ob_get_contents();
            ob_clean();
        }
        ob_end_clean();
        echo strtr($this->template,$tr);
    }

    
    protected function initDefaultButtons() {
        parent::initDefaultButtons();
        
        if (Yii::app()->request->enableCsrfValidation) {
            $csrfTokenName = Yii::app()->request->csrfTokenName;
            $csrfToken = Yii::app()->request->csrfToken;
            $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
        }
        else
            $csrf = '';
        
        
        foreach ($this->buttons as $id => $button) {
            if (($id != 'view') && ($id != 'update') && ($id != 'delete')) {
                // not default buttons ( user defined )
                
                if (isset($button['ajax']) && ($button['ajax'])) {
                    
                    $this->buttons[$id]['click'] = <<<EOD
function() {
        var t = $(this);
        var icon = t.find('i');
        var pl = icon.attr('pl');
        $.fn.yiiGridView.update('{$this->grid->id}', {
                type:'POST',
                url:$(this).attr('href'),$csrf
                success:function() {
                        $.fn.yiiGridView.update('{$this->grid->id}');
                }
        });
        //icon.removeClass('icon-ok').addClass('icon-lock');
        return false;
}
EOD;
                }
            }
        }
    }

}

?>
